package com.situ.student.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.swing.text.AbstractDocument.BranchElement;

import com.situ.student.entity.User;
import com.situ.student.service.IUserService;
import com.situ.student.service.impl.UserServiceImpl;

/**
 * Servlet implementation class LoginServlet2
 */
public class LoginController extends HttpServlet {
	private IUserService userService = new UserServiceImpl();
	
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.setCharacterEncoding("utf-8");
		String method = req.getParameter("method");
		switch (method) {
		case "login":
			login(req, resp);
			break;
		case "logout":
			logout(req, resp);
			break;
		default:
			break;
		}
	}

	private void logout(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		HttpSession session = req.getSession();
		User user = (User) session.getAttribute("user");
		if (null != user) {
			//销毁Session
			session.invalidate();
			
			//首先从ServletContext域对象中取出List<User> onLineUserList;
			ServletContext servletContext = getServletContext();
			List<User> onLineUserList = (List<User>) servletContext.getAttribute("onLineUserList");
			onLineUserList.remove(user);
			servletContext.setAttribute("onLineUserList", onLineUserList);
			
			//重定向到登录界面
			resp.sendRedirect(req.getContextPath() + "/login.jsp");
		}
	}

	public synchronized void login(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		//1、获取参数
		//用户输入的验证码
		String checkCode = req.getParameter("checkCode");
		String checkCodeSession = 
				(String) req.getSession().getAttribute("checkCodeSession");
		if (null == checkCode 
				|| "".equals(checkCode) 
				|| !checkCode.equalsIgnoreCase(checkCodeSession)) {
			resp.sendRedirect(req.getContextPath() + "/login.jsp");
			return;
		}
		
		//2、业务处理
		String name = req.getParameter("name");
		String password = req.getParameter("password");
		User user = userService.login(name, password);
		//3、转发或者重定向完成页面跳转
		//if (user != null) {
		if (user != null) {
			//登录成功
			//创建Session
			HttpSession session = req.getSession();
			//把数据保存到Session域对象
			session.setAttribute("user", user);
			
			//首先从ServletContext域对象中取出List<User> onLineUserList;
			ServletContext servletContext = getServletContext();
			List<User> onLineUserList = (List<User>) servletContext.getAttribute("onLineUserList");
			/*synchronized (onLineUserList) {
				if (onLineUserList == null) {
					onLineUserList = new ArrayList<>();
				}
			}*/
			if (onLineUserList == null) {
				onLineUserList = new ArrayList<>();
			}
			onLineUserList.add(user);
			servletContext.setAttribute("onLineUserList", onLineUserList);
			
			//登录成功之后重定向到学生列表界面
			resp.sendRedirect(req.getContextPath() + "/student?method=pageList");
		} else {
			//登录失败
			resp.sendRedirect(req.getContextPath() + "/fail.html");
		}
	}

}
